from Common import *
import DB
import DictParamChecker

class Account:
    def __init__(self, logger, global_data):
        self.db = DB.DB(logger)
        self.logger = logger
        self.global_data = global_data

    def __handleSpecialArgs(self, args):
        if 'show_delete' in args and args['show_delete'] == '1':
            args['show_delete'] = True
        else:
            args['show_delete'] = False
        if 'return_raw' in args and args['return_raw'] == '1':
            args['return_raw'] = True
        else:
            args['return_raw'] = False
        return args

    def getAll(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getAllAccountInfo', args)

    def getAllGroupedByCurrency(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getAllAccountInfoOrganizedByCurrency', args)

    def getAllCurrencyAndAccountsInfo(self, args):
        return self.db.call('getAllCurrencyAndAccountsInfo', {})

    def add(self, args):
        checker = DictParamChecker.DictParamChecker()
        checker.addParam('crc_id', 'int', True)
        checker.addParam('typepe', 'int', True)
        checker.addParam('nameme', 'str', True)
        checker.addParam('descriptiontion', 'str', True)
        args = checker.check(args)
        if type(args) == str:
            makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('addAccount', args)

    def delete(self, args):
        if 'acnt_id' not in args:
            return makeReturn(Error.ILLEGAL_ARGS)
        return self.db.call('deleteAccount', args)

    def edit(self, args):
        checker = DictParamChecker.DictParamChecker()
        checker.addParam('acnt_id', 'int', True)
        checker.addParam('nameme', 'str', True)
        args = checker.check(args)
        if type(args) == str:
            makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('editAccount', args)

    